// ************************************************************
// Copyright © 2022
// File Name: main.go
// Author:
// Email:
// Created: 2022-01-23 20:30:09 (CST)
// Last Update:
//          By:
// Description:
// ************************************************************

package main

import (
	"context"
	"fmt"
	"strings"

	"flag"

	"github.com/go-redis/redis/v8"
)

var ctx = context.Background()

func main() {
	var size int
	var count int

	flag.IntVar(&size, "s", 10, "数据大小")
	flag.IntVar(&count, "c", 10000, "数据数量")
	flag.Parse()
	fmt.Printf("我们将创建%5d个键值对，每个值的大小为%5d bytes\n",
		count, size)
	setRedis(size, count)
}

func generateValue(size int) string {
	return strings.Repeat("a", size)
}

func setRedis(size, count int) {
	rdb := redis.NewClient(&redis.Options{
		Addr:     "10.10.1.127:6379",
		Password: "",
		DB:       0,
	})

	_, err := rdb.Ping(ctx).Result()

	if err != nil {
		fmt.Printf("连接Redis出错: %v \n", err)
		return
	}

	value := generateValue(size)
	for i := 0; i < count; i++ {

		err = rdb.Set(ctx, fmt.Sprint(i), value, 0).Err()
		if err != nil {
			panic(err)
		}
	}
}
